package com.winning.pmph.utils;

import com.google.common.collect.Sets;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Set;

/**
 * @author 刘亚飞
 * @Description 取得4个字节的字
 * @create 2021-01-11 15:39
 */
public class FourBytesUtils {

    /**
     * 取得字符串中是4个字节的字符
     *
     * @param value
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getFourBytes(String value) throws UnsupportedEncodingException {
        if (StringUtils.isBlank(value)) {
            return "";
        }
        Set<String> set = Sets.newHashSet();
        byte[] t1 = value.getBytes("UTF-8");
        for (int i = 0; i < t1.length; ) {
            byte tt = t1[i];
            if (CharUtils.isAscii((char) tt)) {
                byte[] ba = new byte[1];
                ba[0] = tt;
                i++;
            }
            if ((tt & 0xE0) == 0xC0) {
                byte[] ba = new byte[2];
                ba[0] = tt;
                ba[1] = t1[i + 1];
                i++;
                i++;
            }
            if ((tt & 0xF0) == 0xE0) {
                byte[] ba = new byte[3];
                ba[0] = tt;
                ba[1] = t1[i + 1];
                ba[2] = t1[i + 2];
                i++;
                i++;
                i++;
            }
            if ((tt & 0xF8) == 0xF0) {
                byte[] ba = new byte[4];
                ba[0] = tt;
                ba[1] = t1[i + 1];
                ba[2] = t1[i + 2];
                ba[3] = t1[i + 3];
                i++;
                i++;
                i++;
                i++;
                set.add(new String(ba));
            }
        }
        return StringUtils.join(set, ",");
    }
}
